< previous page page_416 next page >

Page 416
Instead of writing two separate proceduresone to load the combo box and one to load the list boxyou can write one generic routine that loads either control. Heres the code written in each of the Click events of the command buttons on the sample form:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdCombo_Click ()
   Call StateLoad(cboStates)
End Sub

Private Sub cmdList_Click ()
   Call StateLoad(lstStates)
End Sub
The routine to load either the combo box or list box is very simple for this illustration. The StateLoad() procedure accepts any type of control as the name ctlStates. The ctlStates argument is then used to add items to the list with some hard-coded values:
9f31265bb40b592b1929e18a47ab28e5.gif
Public Sub StateLoad(ctlStates As Control)
   With ctlStates
      .Clear

      .AddItem "Iowa"
      .AddItem "California"
      .AddItem "Arizona"
      .AddItem "Nevada"
      .AddItem "Arkansas"
      .AddItem "Washington"
      .AddItem "Texas"
      .AddItem "Nebraska"
      .AddItem "Utah"
      .AddItem "Kentucky"
   End With
End Sub
Although, literally, any type of control could be passed to this procedure, the only controls that would work are list boxes and combo boxes or any type of control that has an AddItem method. If a text box was passed to this routine, it would bomb as soon as the Clear method was invoked. Therefore it is a good idea to check the type of object in the procedure before using that object.

 
< previous page page_416 next page >

If you like this book, buy it!